﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using PublishData.DAL;

namespace PublishData
{
    public partial class FormThietLapChuoiKetNoi : Form
    {
        public FormThietLapChuoiKetNoi()
        {
            InitializeComponent();
            this.ActiveControl = txtUserName;
        }

        private string ConnectionStringOnGUI
        {
            get
            {
                var connectionString = string.Format("Data Source={0};Initial Catalog={1};User={2};Password={3};MultipleActiveResultSets=True",
                    txtDBServer.Text.Trim(), txtDBName.Text.Trim(), txtUserName.Text, txtPassword.Text);
                return connectionString;
            }
        }

        private static string ReadConnectionString()
        {
            var appPath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
            var configPath = System.IO.Path.Combine(appPath, "app2.bin");
            if (System.IO.File.Exists(configPath) == false)
                return null;
            using (var sr = new System.IO.StreamReader(configPath))
            {
                var st = sr.ReadToEnd();
                var st2 = "";
                for (int i = 0; i < st.Length; i++)
                    st2 += Convert.ToChar(Convert.ToInt32(st[i]) - 255);
                sr.Close();
                return st2;
            }
        }

        private static void WriteConnectionString(string st)
        {
            var appPath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
            var configPath = System.IO.Path.Combine(appPath, "app2.bin");
            using (var sw = new System.IO.StreamWriter(configPath))
            {
                var st2 = "";
                for (int i = 0; i < st.Length; i++)
                    st2 += Convert.ToChar(Convert.ToInt32(st[i]) + 255);
                sw.Write(st2);
                sw.Close();
            }
        }

        public static TCHSHueDataContext AppDbConnection
        {
            get
            {
                // while (true)
                {
                    var connectionString = ReadConnectionString();
                    if (connectionString == null)
                    {
                        (new FormThietLapChuoiKetNoi()).ShowDialog();
                        connectionString = ReadConnectionString();
                        if (connectionString == null)
                            return null;
                    }
                    try
                    {
                        var data = new TCHSHueDataContext(connectionString);
                        //var ds = BLL.UserBLL.getNguoiSuDung("a", "a");
                        return data;
                    }
                    catch
                    {
                        throw new Exception("Chuỗi kết nối không chính xác");
                    }
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                WriteConnectionString(ConnectionStringOnGUI);
                this.Close();
            }
            catch (Exception exc)
            {
                DialogUtils.ShowMessage("Không lưu được thông tin kết nối! Lỗi " + exc.Message);
            }
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            try
            {
                //WriteConnectionString(ConnectionStringOnGUI);
                TCHSHueDataContext data = new TCHSHueDataContext(ConnectionStringOnGUI);
                data.SubmitChanges();
                //var ds = BLL.UserBLL.getNguoiSuDung("a", "a");
                DialogUtils.ShowMessage("Kết nối thành công.");
            }
            catch (Exception exc)
            {
                DialogUtils.ShowError("Kết nối không thành công.\nChi tiết lỗi: " + exc.Message);
            }
        }

        private void btnQuitApplication_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
